from tkinter import Tk, Menu, Toplevel, messagebox
import tkinter as tk
from tkinter.ttk import Combobox
from models import *
from operate_new_sql import *

from PIL import Image, ImageTk


def set_center(width, height, screen_width, screen_height):
    align_str = "%dx%d+%d+%d" % (width, height, (screen_width - width) / 2, (screen_height - height) / 2)
    return align_str


def display_operate():
    def addStudent():
        add_stu_win = Toplevel()
        add_stu_win.title("注册登记")
        width = 400
        height = 300
        add_stu_win.geometry(set_center(width, height, screen_width, screen_height))

        # 添加学生基本信息标签
        stu_name_label = tk.Label(add_stu_win, text="姓名", font=("HeiTi", 14))
        stu_name_label.place(x=50, y=30)

        stu_id_label = tk.Label(add_stu_win, text="学号", font=("HeiTi", 14))
        stu_id_label.place(x=50, y=60)

        stu_gender_label = tk.Label(add_stu_win, text="性别", font=("HeiTi", 14))
        stu_gender_label.place(x=50, y=90)

        stu_grade_label = tk.Label(add_stu_win, text="年级", font=("HeiTi", 14))
        stu_grade_label.place(x=50, y=120)

        stu_major_id_label = tk.Label(add_stu_win, text="专业号", font=("HeiTi", 14))
        stu_major_id_label.place(x=50, y=150)

        # 添加学生基本信息输入框
        stu_name_text = tk.StringVar()
        stu_name_text.set("输入姓名")
        stu_name_entry = tk.Entry(add_stu_win, textvariable=stu_name_text, font=("HeiTi", 14))
        stu_name_entry.place(x=150, y=30)

        stu_id_text = tk.StringVar()
        stu_id_text.set("输入学号")
        stu_id_entry = tk.Entry(add_stu_win, textvariable=stu_id_text, font=("HeiTi", 14))
        stu_id_entry.place(x=150, y=60)

        stu_gender_text = tk.StringVar()
        genders = ["男", "女"]
        stu_gender_combo = Combobox(add_stu_win, textvariable=stu_gender_text, width=16, values=genders,
                                    state='readonly')
        stu_gender_combo.current(0)
        stu_gender_combo.place(x=150, y=90)

        stu_grade_text = tk.StringVar()
        grades = ["大一", "大二", "大三", "大四", "研究生"]
        stu_grade_combo = Combobox(add_stu_win, textvariable=stu_grade_text, width=16, values=grades, state='readonly')
        stu_grade_combo.current(0)
        stu_grade_combo.place(x=150, y=120)

        stu_major_id_text = tk.StringVar()
        stu_major_id_text.set("输入专业ID")
        stu_major_id_entry = tk.Entry(add_stu_win, textvariable=stu_major_id_text, font=("HeiTi", 14))
        stu_major_id_entry.place(x=150, y=150)

        # 添加学生函数
        def add_s():
            name = stu_name_text.get()
            stu_id = int(stu_id_text.get())
            gender = stu_gender_text.get()
            grade = stu_grade_text.get()
            major_id = int(stu_major_id_text.get())
            s = addStu(s_id=stu_id, name=name, major_id=major_id, grade=grade, gender=gender)
            if s == 0:
                messagebox.showerror(title="注册失败", message="信息输入错误!")
                add_stu_win.destroy()
            else:
                messagebox.showinfo(title="注册成功", message="恭喜大连民大又喜提一个小可爱！")
                add_stu_win.destroy()

        # 添加注册和取消返回按键
        stu_reg_btn = tk.Button(add_stu_win, text="注册", font=("HeiTi", 14), command=add_s)
        stu_reg_btn.place(x=80, y=180)

        stu_back_btn = tk.Button(add_stu_win, text="取消", font=("HeiTi", 14), command=add_stu_win.destroy)
        stu_back_btn.place(x=210, y=180)

        add_stu_win.mainloop()

    # 删除学生
    def deleteStu():
        del_stu_win = tk.Toplevel()
        del_stu_win.title("删除学生")
        width = 300
        height = 200
        del_stu_win.geometry(set_center(width, height, screen_width, screen_height))

        # 添加标签
        stu_id_label = tk.Label(del_stu_win, text="学号", font=("HeiTi", 14))
        stu_id_label.place(x=30, y=30)

        # 添加文本框
        stu_id_text = tk.StringVar()
        stu_id_text.set("输入学号")
        stu_id_entry = tk.Entry(del_stu_win, textvariable=stu_id_text, font=("HeiTi", 14))
        stu_id_entry.place(x=80, y=30)

        # 删除学生函数
        def delS():
            stu_id = int(stu_id_text.get())
            s = delStu(stu_id)
            if s == 0:
                messagebox.showerror(title="删除失败", message="学生不存在")
                del_stu_win.destroy()
            else:
                messagebox.showinfo(title="删除成功", message="ByeBye," + s.name)
                del_stu_win.destroy()

        # 添加按键
        del_btn = tk.Button(del_stu_win, text="删除", font=("HeiTi", 14), command=delS)
        del_btn.place(x=40, y=100)

        back_btn = tk.Button(del_stu_win, text="返回", font=("HeiTi", 14), command=del_stu_win.destroy)
        back_btn.place(x=180, y=100)

        del_stu_win.mainloop()

    def addScores():
        add_sco_win = tk.Toplevel()
        width = 400
        height = 250
        add_sco_win.geometry(set_center(width, height, screen_width, screen_height))

        # 添加标签
        stu_id_label = tk.Label(add_sco_win, text="学号", font=("HeiTi", 14))
        stu_id_label.place(x=30, y=30)

        co_id_label = tk.Label(add_sco_win, text="课程名", font=("HeiTi", 14))
        co_id_label.place(x=30, y=60)

        term_label = tk.Label(add_sco_win, text="学期", font=("HeiTi", 14))
        term_label.place(x=30, y=90)

        score_label = tk.Label(add_sco_win, text="分数", font=("HeiTi", 14))
        score_label.place(x=30, y=120)

        # 添加输入框
        stu_id_text = tk.StringVar()
        stu_id_text.set("输入学号")
        stu_id_entry = tk.Entry(add_sco_win, textvariable=stu_id_text, font=("HeiTi", 14))
        stu_id_entry.place(x=120, y=30)

        co_name_text = tk.StringVar()
        co_name_text.set("输入课程名")
        co_name_entry = tk.Entry(add_sco_win, textvariable=co_name_text, font=("HeiTi", 14))
        co_name_entry.place(x=120, y=60)

        term_text = tk.StringVar()
        terms = ["上学期", "下学期"]
        term_combo = Combobox(add_sco_win, textvariable=term_text, width=16, values=terms, state='readonly')
        term_combo.current(0)
        term_combo.place(x=120, y=90)

        score_text = tk.StringVar()
        score_text.set("输入分数")
        score_entry = tk.Entry(add_sco_win, textvariable=score_text, font=("HeiTi", 14))
        score_entry.place(x=120, y=120)

        def addSc():
            s_id = stu_id_text.get()
            c_name = co_name_text.get()
            term = term_text.get()
            score = score_text.get()
            s = addScore(s_id, c_name, term, score)
            if s:
                messagebox.showinfo(title="成功", message="成绩已加入")
                add_sco_win.destroy()
            else:
                messagebox.showerror(title="添加失败", message="信息输入错误！")
                add_sco_win.destroy()

        # 添加按键
        add_button = tk.Button(add_sco_win, text="添加", font=("HeiTi", 14), command=addSc)
        add_button.place(x=100, y=180)

        back_btn = tk.Button(add_sco_win, text="返回", font=("HeiTi", 14), command=add_sco_win.destroy)
        back_btn.place(x=200, y=180)

        add_sco_win.mainloop()

    def updateInfo():
        update_info_win = tk.Toplevel()
        update_info_win.title("更新学籍")
        width = 400
        height = 250
        update_info_win.geometry(set_center(width, height, screen_width, screen_height))
        # 添加学生基本信息标签
        stu_id_label = tk.Label(update_info_win, text="学号", font=("HeiTi", 14))
        stu_id_label.place(x=50, y=30)

        stu_name_label = tk.Label(update_info_win, text="姓名", font=("HeiTi", 14))
        stu_name_label.place(x=50, y=60)

        stu_gender_label = tk.Label(update_info_win, text="性别", font=("HeiTi", 14))
        stu_gender_label.place(x=50, y=90)

        stu_grade_label = tk.Label(update_info_win, text="年级", font=("HeiTi", 14))
        stu_grade_label.place(x=50, y=120)

        stu_major_id_label = tk.Label(update_info_win, text="专业号", font=("HeiTi", 14))
        stu_major_id_label.place(x=50, y=150)

        # 添加学生基本信息输入框
        stu_id_text = tk.StringVar()
        stu_id_text.set("输入学号")
        stu_id_entry = tk.Entry(update_info_win, textvariable=stu_id_text, font=("HeiTi", 14))
        stu_id_entry.place(x=150, y=30)

        stu_name_text = tk.StringVar()
        stu_name_text.set("输入姓名")
        stu_name_entry = tk.Entry(update_info_win, textvariable=stu_name_text, font=("HeiTi", 14))
        stu_name_entry.place(x=150, y=60)

        stu_gender_text = tk.StringVar()
        genders = ["男", "女"]
        stu_gender_combo = Combobox(update_info_win, textvariable=stu_gender_text, width=16, values=genders,
                                    state='readonly')
        stu_gender_combo.current(0)
        stu_gender_combo.place(x=150, y=90)

        stu_grade_text = tk.StringVar()
        grades = ["大一", "大二", "大三", "大四", "研究生"]
        stu_grade_combo = Combobox(update_info_win, textvariable=stu_grade_text, width=16, values=grades,
                                   state='readonly')
        stu_grade_combo.current(0)
        stu_grade_combo.place(x=150, y=120)

        stu_major_id_text = tk.StringVar()
        stu_major_id_text.set("输入专业ID")
        stu_major_id_entry = tk.Entry(update_info_win, textvariable=stu_major_id_text, font=("HeiTi", 14))
        stu_major_id_entry.place(x=150, y=150)

        def update_s():
            id = stu_id_text.get()
            name = stu_name_text.get()
            gender = stu_gender_text.get()
            grade = stu_grade_text.get()
            major_id = stu_major_id_text.get()
            s = updateStu(id=id, name=name, major_id=major_id, gender=gender, grade=grade)
            if s == 0:
                messagebox.showerror(title="更新失败", message="信息输入有误!")
                update_info_win.destroy()
            else:
                messagebox.showinfo(title="更新成功", message="恭喜， 已经成功更新！")
                update_info_win.destroy()

        confirm_btn = tk.Button(update_info_win, text="确认更新", font=("HriTi", 14), command=update_s)
        confirm_btn.place(x=100, y=200)
        back_btn = tk.Button(update_info_win, text="返回", font=("HeiTi", 14), command=update_info_win.destroy)
        back_btn.place(x=230, y=200)

        update_info_win.mainloop()

    # 简历登记函数
    def add_Resume():
        add_resume_win = tk.Toplevel()
        add_resume_win.title("更新学籍")
        width = 300
        height = 300
        add_resume_win.geometry(set_center(width, height, screen_width, screen_height))

        tk.Label(add_resume_win, text="学号", font=("HeiTi", 14)).grid(row=1, column=0)
        tk.Label(add_resume_win, text="姓名", font=("HeiTi", 14)).grid(row=2, column=0)
        tk.Label(add_resume_win, text="年龄", font=("HeiTi", 14)).grid(row=3, column=0)
        tk.Label(add_resume_win, text="性别", font=("HeiTi", 14)).grid(row=4, column=0)
        tk.Label(add_resume_win, text="地区", font=("HeiTi", 14)).grid(row=5, column=0)
        tk.Label(add_resume_win, text="学制", font=("HeiTi", 14)).grid(row=6, column=0)
        tk.Label(add_resume_win, text="政治面貌", font=("HeiTi", 14)).grid(row=7, column=0)
        tk.Label(add_resume_win, text="奖惩记录", font=("HeiTi", 14)).grid(row=8, column=0)
        tk.Label(add_resume_win, text="休复转退", font=("HeiTi", 14)).grid(row=9, column=0)

        stu_id_text = tk.StringVar()
        stu_id_text.set("输入学号")
        stu_id_entry = tk.Entry(add_resume_win, textvariable=stu_id_text, font=("HeiTi", 14))
        stu_id_entry.grid(row=1, column=1)

        stu_name_text = tk.StringVar()
        stu_name_text.set("输入姓名")
        stu_name_entry = tk.Entry(add_resume_win, textvariable=stu_name_text, font=("HeiTi", 14))
        stu_name_entry.grid(row=2, column=1)

        stu_age_text = tk.StringVar()
        stu_age_text.set("输入年龄")
        stu_age_entry = tk.Entry(add_resume_win, textvariable=stu_age_text, font=("HeiTi", 14))
        stu_age_entry.grid(row=3, column=1)

        stu_gender_text = tk.StringVar()
        genders = ["男", "女"]
        stu_gender_combo = Combobox(add_resume_win, textvariable=stu_gender_text, width=16, values=genders,
                                    state='readonly')
        stu_gender_combo.current(0)
        stu_gender_combo.grid(row=4, column=1)

        stu_region_text = tk.StringVar()
        regions = ['河北省', '山西省', '辽宁省', '吉林省',
                   '黑龙江省', '江苏省', '浙江省', '安徽省',
                   '福建省', '江西省', '山东省', '河南省',
                   '湖北省', '湖南省', '广东省', '海南省',
                   '四川省', '贵州省', '云南省', '陕西省',
                   '甘肃省', '青海省', '台湾省', '内蒙古自治区',
                   '广西壮族自治区', '西藏自治区', '宁夏回族自治区', '新疆维吾尔自治区',
                   '北京市', '天津市', '上海市', '重庆市',
                   '香港特别行政区', '澳门特别行政区']
        region_combo = Combobox(add_resume_win, textvariable=stu_region_text, width=16, values=regions,
                                state='readonly')
        region_combo.current(0)
        region_combo.grid(row=5, column=1)

        l_o_s_text = tk.StringVar()
        l_o_ss = ["两年制", "三年制", "四年制", "五年制"]
        l_o_s_combo = Combobox(add_resume_win, textvariable=l_o_s_text, width=16, values=l_o_ss, state='readonly')
        l_o_s_combo.current(0)
        l_o_s_combo.grid(row=6, column=1)

        po_af_text = tk.StringVar()
        po_afs = ["群众", "共青团员", "预备党员", "党员"]
        po_af_combo = Combobox(add_resume_win, textvariable=po_af_text, width=16, values=po_afs, state='readonly')
        po_af_combo.current(0)
        po_af_combo.grid(row=7, column=1)

        re_and_pu_text = tk.StringVar()
        re_and_pu_text.set("输入奖惩记录")
        re_and_pu_entry = tk.Entry(add_resume_win, textvariable=re_and_pu_text, font=("HeiTi", 14))
        re_and_pu_entry.grid(row=8, column=1)

        special_text = tk.StringVar()
        special_text.set("输入休复转退记录")
        special_entry = tk.Entry(add_resume_win, textvariable=special_text, font=("HeiTi", 14))
        special_entry.grid(row=9, column=1)

        # 添加简历函数
        def addRe():
            s_id = int(stu_id_text.get())
            s_name = stu_name_text.get()
            length_of_schooling = l_o_s_text.get()
            region = stu_region_text.get()
            age = int(stu_age_text.get())
            political_affiliation = po_af_text.get()
            s_gender = stu_gender_text.get()
            reward_and_punishment = re_and_pu_text.get()
            special = special_text.get()

            r = addResume(s_id=s_id, name=s_name, region=region, age=age, gender=s_gender,
                          reward_and_punishment=reward_and_punishment,
                          political_affiliation=political_affiliation, length_of_schooling=length_of_schooling,
                          special=special)
            if r == 0:
                messagebox.showerror(title="登记失败", message="输入有误")
                add_resume_win.destroy()
            else:
                messagebox.showinfo(title="登记成功", message="恭喜又多了一位有简历的同学")
                add_resume_win.destroy()

        confirm_btn = tk.Button(add_resume_win, text="确认登记", font=("HriTi", 14), command=addRe)
        confirm_btn.grid(row=10, column=0)
        back_btn = tk.Button(add_resume_win, text="返回", font=("HeiTi", 14), command=add_resume_win.destroy)
        back_btn.grid(row=10, column=1)

        add_resume_win.mainloop()

    my_window = Tk()

    my_window.title("学籍管理系统")

    screen_width, screen_height = my_window.maxsize()

    width = 1000
    height = 800

    align_str = "%dx%d+%d+%d" % (width, height, (screen_width - width) / 2, (screen_height - height) / 2)

    my_window.geometry(align_str)

    my_window.resizable(width=True, height=True)

    # 表格
    tree = tk.ttk.Treeview(my_window)
    tree["columns"] = ("姓名", "学号", "性别", "年级", "专业号")
    tree.column("姓名", width=120)
    tree.column("学号", width=120)
    tree.column("性别", width=120)
    tree.column("年级", width=120)
    tree.column("专业号", width=120)

    tree.heading("姓名", text="x姓名")
    tree.heading("学号", text="x学号")
    tree.heading("性别", text="x性别")
    tree.heading("年级", text="x年级")
    tree.heading("专业号", text="x专业号")

    stus = Student.query.all()
    length = len(stus)
    for i in range(length - 1, -1, -1):
        name = stus[i].name
        sid = stus[i].id
        gender = stus[i].gender
        grade = stus[i].grade
        major_id = stus[i].major_id
        tree.insert("", 0, text="list" + str(1 + i), values=(name, sid, gender, grade, major_id))
    tree.pack()

    # 自定义菜单
    menu_bar = Menu(my_window)
    # 创建登记菜单
    stu_menu = Menu(menu_bar)
    stu_menu.add_command(label="注册登记", command=addStudent)
    stu_menu.add_command(label="删除学生", command=deleteStu)
    stu_menu.add_command(label="成绩登记", command=addScores)
    stu_menu.add_command(label="学籍更改登记", command=updateInfo)
    stu_menu.add_command(label="简历登记", command=add_Resume)

    menu_bar.add_cascade(label="学生登记", menu=stu_menu)

    def showImg(load):
        render = ImageTk.PhotoImage(load)
        img = Label(my_window, image=render)
        img.image = render
        img.place(x=30, y=230)

    def by_score():
        by_score_win = tk.Toplevel()
        by_score_win.title("更新学籍")
        width = 350
        height = 300
        by_score_win.geometry(set_center(width, height, screen_width, screen_height))
        by_score_win.title("成绩统计")

        tk.Label(by_score_win, text="专业名", font=("HeiTi", 14)).place(x=30, y=30)
        tk.Label(by_score_win, text="年级", font=("HeiTi", 14)).place(x=30, y=60)
        tk.Label(by_score_win, text="学期", font=("HeiTi", 14)).place(x=30, y=90)

        stu_grade_text = tk.StringVar()
        grades = ["大一", "大二", "大三", "大四", "研究生"]
        stu_grade_combo = Combobox(by_score_win, textvariable=stu_grade_text, width=16, values=grades,
                                   state='readonly')
        stu_grade_combo.current(0)
        stu_grade_combo.place(x=100, y=60)

        ma_name_text = tk.StringVar()
        ma_name_text.set("输入专业名")
        ma_name_entry = tk.Entry(by_score_win, textvariable=ma_name_text, font=("HeiTi", 14))
        ma_name_entry.place(x=100, y=30)

        term_text = tk.StringVar()
        terms = ["上学期", "下学期"]
        term_combo = Combobox(by_score_win, textvariable=term_text, width=16, values=terms, state='readonly')
        term_combo.current(0)
        term_combo.place(x=100, y=90)

        def bySc():
            show_pie_by_score(ma_name_text.get(), stu_grade_text.get(), term_text.get())
            load = Image.open("./by_score.png")
            if load:
                showImg(load)
                messagebox.showinfo(title="统计成功", message="请返回主界面查看统计图")
                by_score_win.destroy()
            else:
                messagebox.showerror(title="统计失败", message="信息输入有误！")
                by_score_win.destroy()

        confirm_btn = tk.Button(by_score_win, text="确认更新", font=("HriTi", 14), command=bySc)
        confirm_btn.place(x=50, y=140)
        back_btn = tk.Button(by_score_win, text="返回", font=("HeiTi", 14), command=by_score_win.destroy)
        back_btn.place(x=150, y=140)

        by_score_win.mainloop()

    def by_age():

        show_pie_by_age()
        load = Image.open("./by_age.png")
        if load:
            showImg(load)
            messagebox.showinfo(title="统计成功", message="请返回主界面查看结果！")
        else:
            messagebox.showerror(title="统计失败", message="无数据！")

    def by_region():
        show_pie_by_region()
        load = Image.open("./by_region.png")
        if load:
            showImg(load)
            messagebox.showinfo(title="统计成功", message="请返回主界面查看结果！")
        else:
            messagebox.showerror(title="统计失败", message="无数据！")

    # 资料统计菜单
    statistic_menu = Menu(menu_bar)
    statistic_menu.add_command(label="以成绩统计", command=by_score)
    statistic_menu.add_command(label="以年龄统计", command=by_age)
    statistic_menu.add_command(label="以地区统计", command=by_region)

    menu_bar.add_cascade(label="资料统计", menu=statistic_menu)

    # 进行配置项设置
    my_window.config(menu=menu_bar)

    my_window.mainloop()
